﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Kaiser.Extensions;
using Kaiser.Infrastructure.Controllers;
using Kaiser.Models;
using Kaiser.Models.JsonModels;
using Kaiser.Modules.Cubic.DomainModel.Product;
using Kaiser.Modules.Cubic.Services;
using Kaiser.Modules.Cubic.ViewModel;

namespace Kaiser.Modules.Cubic.Controllers
{
    public class ProductCategoryManageController : KaiserBaseController
    {
        private readonly IProductCategoryService _productCategoryService;
        private readonly KaiserBaseControllerHelp<ProductCategory> _help;
        public ProductCategoryManageController(IProductCategoryService productCategoryService)
        {
            _productCategoryService = productCategoryService;

            _help = new KaiserBaseControllerHelp<ProductCategory>(this, _productCategoryService);
        }

        #region 分类管理
        public ActionResult CategoryList()
        {
            return View();
        }


        [HttpPost]
        public ActionResult CategoryList(ProductCategorySearch search, PagingInfo pageinfo, FormCollection form)
        {
            var result = _productCategoryService.SearchProductCategory(search, pageinfo);
            return Content(result.ToJsonString());
        }

        public ActionResult GetCategoryTreeList(PagingInfo pageinfo, FormCollection form)
        {
            IList<DomainModel.Product.ProductCategory> result = _productCategoryService.GetCategoryTreeList();
            return Content(result.ToJson());
        }

        public ActionResult AddCategory(string parentId, string parentName)
        {
            var model = new ProductCategory();
     

            return View("EditCategory", model);

        }

        public ActionResult EditCategory(string id)
        {
            return _help.GetModel(id);
        }

        [HttpPost]
        public ActionResult AddCategory(ProductCategory model, FormCollection form)
        {
            model.CreateMan = CurrentUser.Name;
            model.CreateTime = DateTime.Now;
           // model.ParentId = string.IsNullOrEmpty(model.ParentId) ? "0" : model.ParentId;
            return _help.AddModel(model);
        }

        [HttpPost]
        public ActionResult EditCategory(ProductCategory model, FormCollection form)
        {
 

            return _help.EditModel(model.Id, model, form);
        }

        /// <summary>
        /// 批量删除点餐
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelCategoryDetail(IList<string> ids)
        {
            return _help.DelModel(ids);
        }

        [HttpPost]
        public ActionResult GetCategoryTree(ProductCategorySearch search)
        {
            var pageinfo = new PagingInfo();
            pageinfo.Size = 10000;


            var result = _productCategoryService.GetProductCategoryAll(new ProductCategorySearch(), pageinfo);
            var tree = JsonTree.ConvertToJsonTree(result.TableResult, "CategoryName", "Id", "ParentId","0");

            return Json(tree);
        }

        #endregion

    }
}
